package com.bawei.mvp.http.call;


import com.bawei.mvp.http.bean.DataBean;
import com.bawei.mvp.http.bean.ErrorBean;
import com.blankj.utilcode.util.LogUtils;


import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * 1.重新定义回调
 * 2.处理DataBean code处理
 * 3.错误处理
 */
public abstract class CallObserver<T> implements Observer<DataBean<T>>,IObserver<T> {
    @Override
    public void onSubscribe(@NonNull Disposable d) {

    }

    @Override
    public void onNext(@NonNull DataBean<T> dataBean) {
        //网络通信成功，数据相关code
        if (dataBean.ret == 1 || dataBean.code == 0){
            //数据获取成功
            success(dataBean.data);
        }else if (dataBean.ret == 20001){
            //登录失效 -> 调整登录页码

        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        LogUtils.e(e.getMessage());
        if (e.getMessage().contains("Failed to connect")){
            fail(Error.getErrorBean(Error.NOT_NETWORK));
        }
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void fail(ErrorBean errorBean) {
        //1网络

    }
}
